import AntModal from "@/components/pt/dialog/AntModal";
import {
  getBizLoanBill,
  addBizLoanBill,
  updateBizLoanBill,
  findMaxSort,
} from "@/api/biz/bizLoanBill";

export default {
  name: "CreateForm",
  props: {
    loanTypeOptions: {
      type: Array,
      required: true,
    },

    isOverdueOptions: {
      type: Array,
      required: true,
    },

    isPayAllOptions: {
      type: Array,
      required: true,
    },
  },
  components: {
    AntModal,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加还款信息",
      // 表单参数
      form: {},
      rules: {},
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        beginTime: undefined,

        loanType: undefined,

        endTime: undefined,

        payDate: undefined,

        months: undefined,

        principal: undefined,

        lv: undefined,

        monthPayment: undefined,

        totalPayment: undefined,

        totalInterest: undefined,

        isOverdue: undefined,

        isPayAll: undefined,

        overdueDays: undefined,

        overdueRemark: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加还款信息";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizLoanBillId = row.id;
      getBizLoanBill(bizLoanBillId).then((response) => {
        this.form = response.data;
        this.formTitle = "修改还款信息";
        this.spinning = !this.spinning;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form));
          if (this.form.id !== undefined) {
            updateBizLoanBill(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            addBizLoanBill(saveForm).then((response) => {
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/loanbill/index";
      this.$router.push(index);
    },
  },
};
